/**
 * Created by LeungZ on 2016/10/17.
 */
import logTmp from './form/log.html';
import logCtrl from './form/log.ctrl.js';
import alertTmp from '../../form/alert.html';
import alertCtrl from '../../form/alert.ctrl.js';
import advTmp from './form/advance.html';
import advCtrl from './form/advance.ctrl.js'

class Ctrl {
    constructor($uiModal, appSrv, ctgNotification, $scope, $timeout, serviceSrv) {
        "ngInject";
        this._uiModal = $uiModal;
        this._appSrv = appSrv;
        this._notification = ctgNotification;
        this._serviceSrv = serviceSrv;
        this._timeout = $timeout;
        this.timer = {};
        this._ser = $scope.ser;

        if (!this._ser.verTable) {
            this.getList();
        }

        $scope.$on('$destroy', ()=> {
            angular.forEach(Object.keys(this.timer), timeId=> {
                this._timeout.cancel(this.timer[timeId]);
            })
        })
    }

    getList() {
        this._appSrv.getVerOfService({
            id: this._ser.serviceId,
            currentPage: this._ser.currentPage,
            pageSize: this._ser.pageSize
        }).then(data=> {
            this._ser.verTable = data.list;
            angular.forEach(this._ser.verTable, ver=> {
                if (ver.releaseState == '001' || ver.releaseState == '000') {
                    this.checkState(ver);
                }
                if ((ver.releaseState == '000' || ver.releaseState == '002') && ver.state == '001') {
                    ver.podInfo = {loading: true};
                    this.getPodInfo(ver);
                }
            });
        }).catch(data=> {
            this._ser.verTable = [];
            this._notification.error('获取服务版本列表数据有误.');
        });
    }

    openLogForm(elem) {
        this._uiModal.open({
            animation: true,
            templateUrl: logTmp,
            controller: logCtrl,
            controllerAs: 'vm',
            size: 'lg',
            resolve: {
                yaml: function () {
                    return elem.yamlContent
                }
            }
        });
    }


    openAdvanceForm(service, index) {
        this._uiModal.open({
            templateUrl: advTmp,
            controller: advCtrl,
            controllerAs: 'service',
            size: 'lg',
            resolve: {
                service: function () {
                    return {
                        serviceId: service.serviceId,
                        url: service.url
                    }
                },
                version: function () {
                    return service.verTable[index]
                },
                updateVer: function () {
                    return function (data) {
                        angular.extend(service.verTable[index], data);
                    }
                }
            }
        })
    }


    getPodInfo(ver) {
        this._serviceSrv.getRCDetail({
            name: ver.rcName,
            namesp: ver.namespace,
            code: ver.k8sCode
        }).then(data=> {
            ver.podInfo.loading = false;
            ver.podInfo.running = data.podInfo.running;
            ver.podInfo.pending = data.podInfo.pending;
            ver.podInfo.failed = data.podInfo.failed;
        })
    }

    checkState(elem) {
        (function updateState() {
            this._appSrv.getVerDetail(elem.versionId).then(data=> {
                elem.state = data.state;
                elem.releaseState = data.releaseState;
                elem.releaseDesc = data.releaseDesc;
                if (elem.releaseState == '001' || elem.releaseState == '000') {
                    this.timer[elem.versionId] = this._timeout(updateState.bind(this), 5000);
                }
                if ((elem.releaseState == '002' || elem.releaseState == '000') && elem.state == '001') {
                    this.getPodInfo(elem);
                }
            })
        }).bind(this)()
    }

    deleteVer(elem) {
        this._uiModal.open({
            templateUrl: alertTmp,
            controller: alertCtrl,
            controllerAs: 'vm',
            windowClass: 'alert-dialog alert-dialog-ctg',
            resolve: {
                alert: function () {
                    return {
                        appCode: elem.rcName,
                        action: '移除'
                    }
                }
            }
        }).result.then(()=> {
                elem.state = "loading";
                this._appSrv.deleteVer(elem.versionId,elem.namespace).then(data=> {
                    this._notification.success('移除服务版本成功.');
                    this.getList();
                }).catch(data=> {
                    elem.state = '002';
                    this._notification.error('移除服务版本失败. ' + data.resultMsg);
                })
            });
    }

    stopVer(elem) {
        this._uiModal.open({
            templateUrl: alertTmp,
            controller: alertCtrl,
            controllerAs: 'vm',
            windowClass: 'alert-dialog alert-dialog-ctg',
            resolve: {
                alert: function () {
                    return {
                        appCode: elem.rcName,
                        action: '停用'
                    }
                }
            }
        }).result.then(()=> {
                elem.state = "loading";
                this._appSrv.stopVer(elem.versionId,elem.namespace).then(data=> {
                    this._notification.success('停用服务版本成功.');
                    elem.state = '002';
                    elem.podInfo = null;
                }).catch(data=> {
                    elem.state = '001';
                    this._notification.error('停用服务版本失败. ' + data.resultMsg);
                })
            });
    }

    startVer(elem) {
        elem.state = "loading";
        this._appSrv.startVer(elem.versionId,elem.namespace).then(data=> {
            this._notification.success('启用服务版本成功.');
            elem.state = '001';
            elem.podInfo = {loading: true};
            this.getPodInfo(elem);
        }).catch(data=> {
            elem.state = '002';
            this._notification.error('启用服务版本失败. ' + data.resultMsg);
        })
    }
}
export default Ctrl